/**
 * Created by Administrator on 2016/10/10.
 */
$(function(){
    var index = document.getElementById('index');
    var commodity_detail = document.getElementById('commodity_detail');
    var commodity_list = document.getElementById('commodity_list');
    var shoppingCart_list = document.getElementById('shoppingCart_list');
    var flash_sale_list = document.getElementById('flash_sale_list');
    var shopdetail = document.getElementById('shopdetail');
    //首页js
    if(index){
        shopping.get('shop/index',{uid:$.cookie('local_userId')},function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                //banner
                var focus_img = data.focus_img
                $.each(focus_img,function(i,t){
                    $('.banner .swiper-wrapper').append('<div class="swiper-slide swiper-slide-active" style="width: 100%;"><img data-id="'+ t.product_id+'" src="'+ t.pic_url+'" class=""></div>')
                });
                //商品分类
                var top_l = data.top.length
                var top = data.top;
                var page = Math.ceil(top_l/6);
                var page_all ='';
                for(var i =1;i<=page;i++){
                    var g = (i-1)*6;
                    page_all =  page_all+'<div class="swiper-slide swiper-slide-active" style="width: 100%;"><div class="content">';
                    for(var j=g;j<=g+5;j++){
                        if(j<top_l){
                            page_all = page_all+ '<div class="box" data-id="'+top[j].position+'"><img src="'+top[j].max_pic_url+'"><p>'+top[j].pic_text+'</p></div>  ';
                        }
                    }
                    page_all = page_all+'</div></div>';
                }
                $('#commodity').append(page_all);
                //
                var mySwiper = new Swiper ('.swiper-container', {
                    direction: 'horizontal',
                    loop: false,
                    // 如果需要分页器
                    pagination: '.swiper-pagination'
                });
            //    限时抢购列表
                var promotion_list = data.promotion_list;
                $.each(promotion_list,function(i,t){
                    $('.shopping_index .flash_sale').append('<div class="list" data-id="'+ t.product_id+'"><div class="left"><img src="'+ t.goods_thumb+'" alt=""><div class="time" id="countdown"><div class="date public"><span>00</span></div><div class="dd">:</div><div class="hour public"><span>00</span></div><div class="dd">:</div><div class="minute public"><span>00</span></div><div class="dd">:</div><div class="second public"><span>00</span></div><div class="dd"></div></div></div><div class="right"><p class="introduce">'+ t.product_name+'</p><div class="price"><span class="span1">抢购价</span><span class="span2">￥'+ t.promote_price+'</span><del>'+ t.origin_price+'</del></div></div></div>');
                    //    点击进入详情
                    $('.shopping_index .flash_sale .list').click(function(){
                        var id = $(this).attr('data-id');
                        location.href = 'commodity_detail.html?id='+id;
                    });
                    var time = t.diff_promote_end_date;
                    setInterval(function(){
                        time--
                        if(time>0){
                            var date  =  parseInt(time/(60*60*24));
                            var hours   =  parseInt(time/(60*60)%24);
                            var minute= parseInt(time/60%60);
                            var second=  parseInt(time%60);
                            $('.shopping_index .flash_sale .list').eq(i).find('.time').html('<div class="date public"><span>0</span>'+date+'</div><div class="dd">:</div><div class="hour public"><span>0</span>'+hours+'</div><div class="dd">:</div><div class="minute public"><span>0</span>'+minute+'</div><div class="dd">:</div><div class="second public"><span>0</span>'+second+'</div><div class="dd"></div>');
                            if(second>9){
                                $('.shopping_index .flash_sale .list').eq(i).find('.time').children('.second').find('span').hide();
                            }
                            if(minute>9){
                                $('.shopping_index .flash_sale .list').eq(i).find('.time').children('.minute').find('span').hide();
                            }
                            if(hours>9){
                                $('.shopping_index .flash_sale .list').eq(i).find('.time').children('.hour').find('span').hide();
                            }
                            if(date>9){
                                $('.shopping_index .flash_sale .list').eq(i).find('.time').children('.date').find('span').hide();
                            }
                        }else{
                            $('.shopping_index .flash_sale .list').eq(i).find('.time').html('<div class="date public"><span>00</span></div><div class="dd">:</div><div class="hour public"><span>00</span></div><div class="dd">:</div><div class="minute public"><span>00</span></div><div class="dd">:</div><div class="second public"><span>00</span></div><div class="dd"></div>')
                        }
                    },1000);
                });
            }
        });
        //搜索框固定
        //$(window).scroll(function() {
        //    var st = $('body').scrollTop();
        //    var search_h = $('.shopping_index .searchbox').outerHeight();
        //    if(st>search_h){
        //        $('.shopping_index .searchbox').addClass('hover').removeClass('hover2')
        //    }else{
        //        $('.shopping_index .searchbox').addClass('hover2').removeClass('hover')
        //    }
        //});
        //点击分类进入分类详情
        $(document).on('click','.shopping_index .commodity .content .box',function(){
            var id = $(this).data('id');
            location.href = 'commodity_list.html?id='+id;
        });

    //    购物车数量
        shopping.goodsum('shop/cart_list',{uid:$.cookie('local_userId')});
        //首页点击搜索
        $('.searchbox button').click(function(){
            var search_val = $('.searchbox .left input').val()
            if(search_val==''){
                publicJs.layer('请输入关键词')
            }else{
                location.href = 'commodity_list.html?keyword='+search_val
            }
        });
    }
    //商品详情
    else if(commodity_detail){
        var url = 'product/detail';
        var product_id = publicJs.getQueryString('id');
        var obj = {product_id:product_id}
        shopping.commodity_detail(url,obj);
        $('.commodity_detail .brief-layer').on('touchmove', function (event) {
            event.preventDefault();
        });
        $('.commodity_detail>.bot-shop-cart>.add').click(function(){
            $('.commodity_detail .brief-layer').fadeIn();
            $('.commodity_detail .brief-layer .con').animate({'bottom':'0'});
        });
        $('.commodity_detail .brief-layer .bj-click').click(function(){
            var laycon_h = $('.commodity_detail .brief-layer .con').height();
            $('.commodity_detail .brief-layer .con').animate({'bottom':-laycon_h});
            $('.commodity_detail .brief-layer').fadeOut();
        });
        shopping.get('shop/cart_list',{uid:$.cookie('local_userId')},function(res){
            if(res.ret==0){
                console.log(res);
                var d = res.data;
                $('.commodity_detail .bot-shop-cart .num p').html(d.goodsum);
            }else{
                console.log(res.msg);
            }
        });
    //    点击购物车
        $('.commodity_detail .bot-shop-cart .num').click(function(){
            location.href = 'shoppingCart_list.html'
        });
    }
    //商品列表
    else if(commodity_list){
        var uid = $.cookie('local_userId');
        var page_size = 6;
        var page_index = 1;
        var unit_price = 'unit_price_desc';
        var cat_id = publicJs.getQueryString('id');
        var obj = {uid:uid,page_size:page_size,page_index:page_index,sort_index:unit_price,cat_id:cat_id};
        var keyword = publicJs.getQueryString('keyword');
        $('.shopping_index .searchbox .left input').val(keyword)
        if(keyword){
            $('#commodity_list .bar .public').remove();
            var obj = {
                uid:uid,
                page_size:page_size,
                page_index:page_index,
                search_content:keyword
            }
            shopping.search('shop/product',obj);
        }else{
            $('.shopping_index .searchbox').remove();
            shopping.commodity_list('shop/getCategoryShopList',obj);
        }
        $('.shopping_index .searchbox button').click(function(){
            var search_val = $('.shopping_index .searchbox .left input').val()
            if(search_val==''){
                publicJs.layer('请输入关键词')
            }else{
                location.href = 'commodity_list.html?keyword='+search_val
            }
        });
        //商品列表
        myApp = new Framework7();
        $$ = Dom7;
        //        下拉加载
        var loading = false;
        //下拉刷新
        $('.infinite-scroll').on('infinite', function () {
            // 如果正在加载，则退出
            if (loading) return;
            loading = true;
            setTimeout(function () {
                loading = false;
                obj.page_index++
                if(keyword){
                    shopping.search('shop/product',obj)
                }else{
                    shopping.commodity_list('shop/getCategoryShopList',obj);
                }
            }, 1000);
        });
        var offset = $(".shoppingCart").offset();
        $('.commodity_list').on('touchmove',function(){
            $('.fly').remove();
        });
        //    购物车数量
        shopping.goodsum('shop/cart_list',{uid:$.cookie('local_userId')});
        //点击加入购物车
        $(document).on("click",".commodity_list li .price .r",function(event){
            var flyer = $('<div class="fly" style=" z-index:99998;width: 0.3rem; height: 0.3rem;border-radius: 100px; background: #eb752e; color: #eb752e;"></div>');
            event.stopPropagation();
            flyer.fly({
                start: {
                    left: event.pageX,
                    top: event.pageY-30
                },
                end: {
                    left: offset.left-10,
                    top: offset.top,
                    width:5,
                    height: 5
                },
            });
            var shop_num = $('.shoppingCart .shopnum').html();
            shop_num++
            $('.shoppingCart .shopnum').html(shop_num);
            var product_id = $(this).parents('li').attr('data-id');
            var obj = {
                uid:$.cookie('local_userId'),
                product_id:product_id,
                num:1
            }
            shopping.joinshopcart('cart/add',obj)
        });
        //    点击商品列表进详情
        $(document).on('click','.commodity_list li',function(){
            var id= $(this).data('id')
            location.href = 'commodity_detail.html?id='+id;
        });
        //点击价格排序
        $('#commodity_list .bar .public').click(function(){
            var index = $(this).index();

            $(this).css('color','#63ae85').siblings().css('color','#666')
            if(index==0){
                obj.sort_index = 'complex_asc';
            }else if(index==1){
                var hasclick = $(this).hasClass('click')
                if(hasclick){
                    $(this).removeClass('click');
                    $('#commodity_list .bar .price img').attr('src','images/icon5.png');
                    obj.sort_index = 'unit_price_desc';
                }else{
                    $(this).addClass('click');
                    $('#commodity_list .bar .price img').attr('src','images/icon6.png');
                    obj.sort_index = 'unit_price_asc';
                }

            }else if(index==2){
                obj.sort_index = 'sales_asc';
            }
            $('.commodity_list').html('');
            obj.page_index = 1 ;
            shopping.commodity_list('shop/getCategoryShopList',obj);
        });
    }
    //购物车列表
    else if(shoppingCart_list){

        var obj = {
            uid:$.cookie('local_userId')
        }
        shopping.shoppingCart('shop/cart_list',obj)
        //购物车数量加减
        $(document).on('click','.shoppingCart_list .list li .num_control .jian',function(){
            var num = $(this).siblings('.num').html();
            var price = parseInt($(this).parent().data('price'));
            var g_num = $('.shoppingCart_list .toolbar .total .number span').html();
            if($(this).parents('li').hasClass('click')){
                if(g_num==1){
                    publicJs.propmt('宝贝不能在减少了')
                }else{
                    g_num--
                    $('.shoppingCart_list .toolbar .total .number span').html(g_num)
                }
            }
            if(num==1){
                publicJs.propmt('宝贝不能在减少了')
            }else{
                num--
                //$(this).parents('.num_control').siblings('.center').find('.p3').html('￥'+num*price);
                var total = parseInt($('.shoppingCart_list .toolbar .total .money .sp2').html());
                $('.shoppingCart_list .toolbar .total .money .sp2').html(total-price);
            }
            $(this).siblings('.num').html(num);
        });
        //点击加
        $(document).on('click','.shoppingCart_list .list li .num_control .jia',function(){
            var num = $(this).siblings('.num').html();
            var price = parseInt($(this).parent().data('price'));
            var g_num = $('.shoppingCart_list .toolbar .total .number span').html();
            if($(this).parents('li').hasClass('click')){
                g_num++
                $('.shoppingCart_list .toolbar .total .number span').html(g_num)
            }
            num++
            $(this).siblings('.num').html(num);
            //$(this).parents('.num_control').siblings('.center').find('.p3').html('￥'+num*price);
            var total = parseInt($('.shoppingCart_list .toolbar .total .money .sp2').html());
            $('.shoppingCart_list .toolbar .total .money .sp2').html(total+price);
        });
        //点击编辑
        var edit = true;

        $('.shoppingCart_list .consignor .right').click(function(){
            var cart_str = '';
            if(edit==true){
                $(this).html('完成');
                $('.shoppingCart_list .toolbar .del-settlement').css({'background':'#fff','border':'1px solid #44b181','color':'#44b181'}).html('删除');
                $('.shoppingCart_list .list li .num_control').show();
                //$('.shoppingCart_list .toolbar .total').hide();
                edit=false;
            }else{
                $(this).html('编辑');
                $('.shoppingCart_list .toolbar .del-settlement').css({'background':'#44b181','border':'1px solid #fff','color':'#fff'}).html('结算');
                $('.shoppingCart_list .list li .num_control').hide();
                //$('.shoppingCart_list .toolbar .total').show();
                edit=true;
                var li_l = $('.shoppingCart_list .list li').length;
                for(i=0;i<li_l;i++){
                    if($('.shoppingCart_list .list li').eq(i).attr('data-stay')!=1){
                        var num = $('.shoppingCart_list .list li').eq(i).children('.num_control').children('.num').html();
                        var id = $('.shoppingCart_list .list li').eq(i).attr('data-id');
                        cart_str+='{"product_id":'+id+',"num":'+num+'},'
                        console.log(cart_str)
                    }
                }
                //$('.shoppingCart_list .list li .radio').attr('src','../img/icon5.png');
                //$('.shoppingCart_list .list li').removeClass('click');
                //$('.shoppingCart_list .toolbar .radio img').attr('src','../img/icon5.png').removeClass('click');
                //$('.shoppingCart_list .toolbar .radio').removeClass('click');

                cart_str = cart_str.substring(0,cart_str.length-1);
                console.log(cart_str)
                var obj = {
                    uid:$.cookie('local_userId'),
                    cart_str:'['+cart_str+']'
                }
                shopping.cmedit('cart/operatorCartProduct',obj);
            }
        });
    //    点击结算和删除
        $('.shoppingCart_list .toolbar .del-settlement').click(function(){
             if(edit==false){
                 for(var i=0;i<$('.shoppingCart_list .list li').length;i++){
                     if($('.shoppingCart_list .list li').eq(i).hasClass('click')){

                         $('.shoppingCart_list .list li').eq(i).fadeOut().attr('data-stay',1).removeClass('click');
                     }
                 }
                 $('.shoppingCart_list .toolbar .total .money .sp2').html('');
                 $('.shoppingCart_list .toolbar .total .number span').html('0')
             }else{
                 if($('.shoppingCart_list .list li').hasClass('click')){
                     var li_l = $('.shoppingCart_list .list li').length;
                     var product_str = '';
                     for(i=0;i<li_l;i++){
                         if($('.shoppingCart_list .list li').eq(i).attr('data-stay')!=1&&$('.shoppingCart_list .list li').eq(i).hasClass('click')){
                             var num = $('.shoppingCart_list .list li').eq(i).children('.num_control').children('.num').html();
                             var id = $('.shoppingCart_list .list li').eq(i).attr('data-id');
                             var cart_item_id = $('.shoppingCart_list .list li').eq(i).attr('data-item_id');
                             product_str+='{"product_id":'+id+',"num":'+num+',"cart_item_id":'+cart_item_id+'},';
                         }
                     }
                     product_str = product_str.substring(0,product_str.length-1);
                     $.cookie('product_str',product_str,{ expires: 7,path: '/' });
                     console.log($.cookie('product_str'));
                     location.href = '../hospital_nurse/submitorder-shop.html'
                 }else{
                     alert('请选择结算订单');
                 }
             }

            //shopping.cmedit('cart/operatorCartProduct',obj)
        });
    }
    //促销列表
    else if(flash_sale_list){
        console.log(123)
        var page_index = 1;
        var page_size = 5
        var obj = {
            uid:$.cookie('local_userId'),
            page_index:page_index,
            page_size:page_size
        }
        shopping.flash_sale_list('shop/promotion_list',obj);
        myApp = new Framework7();
        $$ = Dom7;
        //        下拉加载
        var loading = false;
        //下拉刷新
        $('.infinite-scroll').on('infinite', function () {
            jzli_l = $('.shopping_index .flash_sale .list').length;
            // 如果正在加载，则退出
            if (loading) return;
            loading = true;
            setTimeout(function () {
                loading = false;
                obj.page_index++
                shopping.flash_sale_list('shop/promotion_list',obj);
            }, 1000);
        });
    }
    //订单详情
    else if(shopdetail){
        var order_id = publicJs.getQueryString('order_id');
        var obj = {
            uid:$.cookie('local_userId'),
            order_id :order_id
        }
        shopping.orderdetail('diagnosis/orderdetail/',obj);
    }
    //    点击购物车选矿
    $(document).on('click','.shoppingCart_list .list li .radio',function(){
        var has = $(this).parent('li').hasClass('click');
        $('.shoppingCart_list .toolbar .radio').find('img').attr('src','../img/icon5.png');
        $('.shoppingCart_list .toolbar .radio').removeClass('click');
        if(has){
            $(this).parent('li').removeClass('click');
            $(this).attr('src','../img/icon5.png');
        }else{
            $(this).parent('li').addClass('click');
            $(this).attr('src','../img/icon6.png');
        }
        var totalarr  = 0;
        var number = 0;
        $('.shoppingCart_list .toolbar .total .money .sp2').html(totalarr);
        $('.shoppingCart_list .toolbar .total .number span').html(number);
        for(var i=0;i<$('.shoppingCart_list .list li').length;i++){
            if($('.shoppingCart_list .list li').eq(i).hasClass('click')){
                var price = $('.shoppingCart_list .list li').eq(i).children('.num_control').attr('data-price');
                var num = parseInt($('.shoppingCart_list .list li').eq(i).children('.num_control').find('.num').html());
                var total = num*price;
                totalarr+=total
                number+=num

                $('.shoppingCart_list .toolbar .total .money .sp2').html(totalarr);
                $('.shoppingCart_list .toolbar .total .number span').html(number);
                console.log(price+'-'+num)
            }
        }
    });
    //    全选
    $('.shoppingCart_list .toolbar .radio').click(function(){
        var has = $(this).hasClass('click');
        if(has){
            $(this).find('img').attr('src','../img/icon5.png');
            $('.shoppingCart_list .list li .radio').attr('src','../img/icon5.png');
            $('.shoppingCart_list .list li').removeClass('click');
            $(this).removeClass('click');
        }else{
            $(this).find('img').attr('src','../img/icon6.png');
            $('.shoppingCart_list .list li .radio').attr('src','../img/icon6.png');
            $('.shoppingCart_list .list li').addClass('click');
            $(this).addClass('click');
        }
        var totalarr  = 0;
        var number = 0;
        for(var i=0;i<$('.shoppingCart_list .list li').length;i++){
            if($('.shoppingCart_list .list li').eq(i).hasClass('click')&&$('.shoppingCart_list .list li').eq(i).attr('data-stay')!=1){
                var price = $('.shoppingCart_list .list li').eq(i).children('.num_control').attr('data-price');
                var num = parseInt($('.shoppingCart_list .list li').eq(i).children('.num_control').find('.num').html());
                var total = num*price;
                totalarr+=total
                number+=num

                $('.shoppingCart_list .toolbar .total .money .sp2').html(totalarr);
                $('.shoppingCart_list .toolbar .total .number span').html(number)
                console.log(price+'-'+num)
            }
        }
    });
    //    点击进入购物车列表
    $('.shoppingCart .icon').click(function(){
         location.href = 'shoppingCart_list.html';
    });
});
url = 'http://ekang.ren/';
shopping = {

//    get
    get : function (texturl,data,success){
        $.ajax({
            type : 'get',
            url : url+texturl,
            dataType : 'jsonp',
            data : data,
            success : function(response) {
                success(response);
            }
        });
    },
//    获取连接参数
    getQueryString : function(name){
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(decodeURI(r[2])); return null;
    },
//    倒计时
    showtime : function(time){
        var www = document.getElementById('countdown');
        //var Newtime = new Date();
        //var endtime = new Date(time);
        //var lefttime=parseInt((endtime.getTime()-Newtime.getTime())/1000);
        var lefttime = time;
        if(lefttime>0){
            var date  =  parseInt(lefttime/(60*60*24));
            var hours   =  parseInt(lefttime/(60*60)%24);
            var minute= parseInt(lefttime/60%60);
            var second=  parseInt(lefttime%60);
            www.innerHTML = '<div class="date public"><span>0</span>'+date+'</div><div class="dd">:</div><div class="hour public"><span>0</span>'+hours+'</div><div class="dd">:</div><div class="minute public"><span>0</span>'+minute+'</div><div class="dd">:</div><div class="second public"><span>0</span>'+second+'</div><div class="dd"></div>';
            if(second>9){
                $('#countdown .second span').hide();
            }
            if(minute>9){
                $('#countdown .minute span').hide();
            }
            if(hours>9){
                $('#countdown .hour span').hide();
            }
            if(date>9){
                $('#countdown .date span').hide();
            }
        }else{
            www.innerHTML = '<div class="date public"><span>00</span></div><div class="dd">天</div><div class="hour public"><span>00</span></div><div class="dd">时</div><div class="minute public"><span>00</span></div><div class="dd">分</div><div class="second public"><span>00</span></div><div class="dd">秒</div>';
        }
    },
//    商品分类
    commodity_list : function(url,obj){
        shopping.get(url,obj,function(res){
            var data = res.data;
            if(res.ret==0){
                console.log(res)
                var product_list = data.product_list;
                if(product_list.length<obj.page_size){
                    $('.infinite-scroll-preloader .preloader').remove();
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                }else{
                    $('.infinite-scroll-preloader').html('');
                    $('.infinite-scroll-preloader').append('<div class="preloader"></div>')
                }
                $.each(product_list,function(i,t){
                    $('.commodity_list').append('<li data-id="'+ t.product_id+'"><img class="comm_pic" src="'+ t.goods_thumb+'" alt=""><p class="p1">'+ t.product_name+'</p><div class="price"><p class="l">￥'+ t.unit_price+'</p><img class="r" src="images/icon1.png"></div><p class="buynum">'+ t.sales+'人已购买</p></li>')
                });
            }else{
                console.log(res.msg)
            }
        });
    },
//    商品详情
    commodity_detail : function(url,obj){
        shopping.get(url,obj,function(res){
            var data = res.data;
            if(res.ret==0){
                console.log(res.data);
                $('.commodity_detail .commodity_introduce').html('<div class="price"><p class="now-price">￥'+data.product_info.unit_price+'</p><del>'+data.product_info.origin_price+'</del><span>'+data.product_info.sales+'人已购</span></div><div class="introduce">'+data.product_info.product_name+'</div>');
                $('.commodity_detail .activity p').html(data.product_info.active_promote_text);
                $('.commodity_detail .brief-introduction .con .name span').html(data.product_info.product_name);

                //轮播图
                $.each(data.product_info.product_images,function(i,t){
                    $('.commodity_detail .banner .swiper-wrapper').append('<div class="swiper-slide swiper-slide-prev" style="width: 100%;"><img style=" width: 100%; height: 100%;" src="'+ t.pic_url+'" class=""></div>');
                });
                var mySwiper = new Swiper ('.swiper-container', {
                    direction: 'horizontal',
                    loop: false,
                    // 如果需要分页器
                    pagination: '.swiper-pagination'
                });
                $('.commodity_detail .brief-introduction .con .article').html(data.product_info.editor_content);

                $('.commodity_detail .brief-layer').attr('data-id',data.product_info.product_id);
                $('.commodity_detail .brief-layer .con .top img').attr('src',data.product_info.goods_thumb);
                $('.commodity_detail .brief-layer .con .top .name-pri .p1').html(data.product_info.product_name);
                $('.commodity_detail .brief-layer .con .top .name-pri .p2').html('￥'+data.product_info.unit_price);
                $('.commodity_detail .brief-layer .con .total .left span').html('￥'+data.product_info.unit_price).attr('data-price',data.product_info.promote_price);

                var all_attr_product = data.all_attr_product;
                var all_product_detail = data.all_product_detail;
                var product_attr = data.product_attr;
                var id = publicJs.getQueryString('id');
                var typeid = all_attr_product[id]
                //循环分类
                $.each(product_attr,function(i,t){
                    var typeitem = '';
                    var attr_product_id = all_attr_product[id][i-1];
                    $.each(t.child_list,function(e,v){
                        typeitem+='<dd data-id="'+v.attr_id+'" id="attr_'+ v.attr_id+'" hight_color="0" >'+ v.attr_name+'</dd>';
                    });
                    $('.commodity_detail .brief-layer .con .type').append('<dl data-id="'+ t.parent_attr_id+'"><dt>'+ t.parent_attr_name+'</dt>'+typeitem+'</dl>');
                });
                //点击选择分类
                $('.commodity_detail .brief-layer .con .type dl dd').click(function() {
                    var dl_id = $(this).parent().data('id');
                    var dd_id = $(this).data('id');
                    $(this).addClass('hover').siblings('dd').removeClass('hover');
                    var dl_length = $('.commodity_detail .brief-layer .con .type dl').length;
                    product = '';
                    //循环dl
                    for(var i=0;i<dl_length;i++){
                        //判断dd是否有class
                        if($('.commodity_detail .brief-layer .con .type dl').eq(i).find('dd').hasClass('hover')){
                            //有class循环出哪一个有，并获取其id
                            var dd_length = $('.commodity_detail .brief-layer .con .type dl').eq(i).find('dd').length;
                            for(var j=0;j<dd_length;j++){
                                if($('.commodity_detail .brief-layer .con .type dl').eq(i).find('dd').eq(j).hasClass('hover')){
                                    var dd_id = $('.commodity_detail .brief-layer .con .type dl').eq(i).find('dd').eq(j).attr('data-id');
                                    //product2.push(dd_id);
                                    product+=dd_id+','
                                    product_l = product.length/2;
                                    var str=product;
                                    var newproduct=product.substring(0,str.length-1);
                                }
                            }
                        }
                    }
                    if(dl_length==product_l){
                        console.log(newproduct)
                        var product_id = $('.commodity_detail .brief-layer').data('id');
                        var obj = {
                            uid:$.cookie('local_userId'),
                            product_id:product_id,
                            attr_str:newproduct
                        }
                        shopping.attribute('product/getProductAttrInfo',obj)
                    }
                    //console.log(product)
                });
                //购买数量加减
                var buynum = 1
                $('.commodity_detail .brief-layer .con .buynum .right .jia').click(function(){
                     var price = $('.commodity_detail .brief-layer .con .total .left span').data('price');
                     buynum++
                     $('.commodity_detail .brief-layer .con .buynum .right .number').html(buynum);
                     $('.commodity_detail .brief-layer .con .total .left span').html('￥'+buynum*price)
                });
                $('.commodity_detail .brief-layer .con .buynum .right .jian').click(function(){
                    var price = $('.commodity_detail .brief-layer .con .total .left span').data('price');
                    if(buynum==1){
                        buynum = 1
                        publicJs.propmt('亲，宝贝不能在减少了')
                    }else{
                        buynum--
                        $('.commodity_detail .brief-layer .con .total .left span').html('￥'+buynum*price)
                    }
                    $('.commodity_detail .brief-layer .con .buynum .right .number').html(buynum);
                });
                //    点击加入购物车
                $('.commodity_detail .brief-layer .con .add').click(function(){
                    var dl_length = $('.commodity_detail .brief-layer .con .type dl').length;
                    var product_id = $('.commodity_detail .brief-layer').attr('data-id');
                    typehtml='';
                    //循环dl
                    for(var i=0;i<dl_length;i++){
                        //判断dd是否有class
                        if(!$('.commodity_detail .brief-layer .con .type dl').eq(i).find('dd').hasClass('hover')){
                            typehtml+=' '+$('.commodity_detail .brief-layer .con .type dl').eq(i).find('dt').html()+' ';
                        }
                    }
                    if(typehtml!=''){
                        publicJs.propmt('请选择'+typehtml+'分类')
                    }else{
                        var obj = {
                            uid:$.cookie('local_userId'),
                            product_id:product_id,
                            num:buynum
                        }
                        shopping.joinshopcart('cart/add',obj)
                    }

                });
                //    点击联系客服
                $('.commodity_detail .bot-shop-cart .customer-service').click(function(){
                    publicJs.layer('客服联系电话：4006305060');
                });
            }else{
                console.log(res.msg)
            }
        });
    },
//    加入购物车
    joinshopcart : function(url,obj){
        shopping.get(url,obj,function(res){

            if(res.ret==0){
                console.log(res)
                var laycon_h = $('.commodity_detail .brief-layer .con').height();
                $('.commodity_detail .brief-layer .con').animate({'bottom':-laycon_h});
                $('.commodity_detail .brief-layer').fadeOut();
                var imgurl = $('.swiper-slide').eq(0).find('img').attr('src');
                if(document.getElementById('commodity_detail')){
                    $('body').append('<div class="joincm"><img src="'+imgurl+'"></div>');
                    var num = parseInt($('.commodity_detail .bot-shop-cart .num p').html());
                    var jia_num = parseInt($('.commodity_detail .brief-layer .con .buynum .right .number').html());
                    $('.commodity_detail .bot-shop-cart .num p').html(num+jia_num)
                    setTimeout(function(){
                        $('.joincm').remove();
                    },1500);
                }
            }else{
                console.log(res.msg)
            }
        });
    },
//    购物车列表
    shoppingCart : function(url,obj){
        shopping.get(url,obj,function(res){
            if(res.ret==0){
                console.log(res);
                var d = res.data;
                $.each(d[0].list,function(i,t){
                    $('.shoppingCart_list .list').append('<li data-item_id="'+ t.cart_item_id+'" data-id="'+ t.product_id+'"><img class="radio" src="../img/icon5.png"><img class="pic" src="'+ t.goods_thumb+'" alt=""><div class="center"><p class="p1">'+ t.product_name+'</p><p class="p2">片</p><p class="p3">￥'+ t.unit_price+'</p></div><div data-price="'+ t.unit_price+'" class="num_control"><div class="jia">+</div><div class="num">'+t.quantity+'</div><div class="jian">-</div></div></li>');
                });
                //$('.shoppingCart_list .toolbar .total .money .sp2').html(d.product_price);
                //$('.shoppingCart_list .toolbar .total .number span').html(d.goodsum);
            }else{
                console.log(res.msg);
            }
        });
    },
//    购物车数量
    goodsum : function(url,obj){
        shopping.get(url,obj,function(res){
            if(res.ret==0){
                console.log(res);
                var d = res.data;
                $('.shoppingCart .shopnum').html(d.goodsum);
            }else{
                console.log(res.msg);
            }
        });
    },
//    商品属性
    attribute : function(url,obj){
        shopping.get(url,obj,function(res){
            if(res.ret==0){
                console.log(res)
                var d = res.data;
                if(res.data==''){
                    $('.commodity_detail .brief-layer .con .add').attr("disabled", true);
                    publicJs.layer('商品已售罄')
                }else{
                    $('.commodity_detail .brief-layer .con .add').attr("disabled", false);
                    $('.commodity_detail .brief-layer').attr('data-id',d.product_id);
                    $('.commodity_detail .brief-layer .con .top img').attr('src',d.goods_thumb);
                    $('.commodity_detail .brief-layer .con .top .name-pri .p1').html(d.product_name);
                    $('.commodity_detail .brief-layer .con .top .name-pri .p2').html('￥'+d.promote_price);
                    var num = $('.commodity_detail .brief-layer .con .buynum .right .number').html();
                    $('.commodity_detail .brief-layer .con .total .left span').html('￥'+d.promote_price*num).attr('data-price',d.promote_price);

                }
            }else{
                console.log(res.msg)
            }
        });
    },
//    商品编辑
    cmedit : function(url,obj){
        shopping.get(url,obj,function(res){
            if(res.ret==0){
                console.log(res)
                //$('.shoppingCart_list .list').html('');
                //var obj = {
                //    uid:1
                //}
                //shopping.shoppingCart('shop/cart_list',obj)
            }else{
                console.log(res.msg)
            }
        });
    },
//    促销列表
    flash_sale_list : function(url,obj){
        shopping.get(url,obj,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                //    限时抢购列表
                $.each(data,function(i,t){
                    var timehtml = '';
                    var time = t.diff_promote_end_date;

                    $('.shopping_index .flash_sale').append('<li class="list"><div class="left"><img src="'+ t.goods_thumb+'" alt=""><div class="time" id="countdown"><div class="date public"><span>00</span></div><div class="dd">:</div><div class="hour public"><span>00</span></div><div class="dd">:</div><div class="minute public"><span>00</span></div><div class="dd">:</div><div class="second public"><span>00</span></div><div class="dd"></div></div></div><div class="right"><p class="introduce">'+ t.product_name+'</p><div class="price"><span class="span1">抢购价</span><span class="span2">￥'+ t.promote_price+'</span><del>'+ t.origin_price+'</del></div></div></li>');
                    var obj2 = $('.shopping_index .flash_sale .list')
                    shopping.showtime2(time,obj2,(obj.page_index-1)*obj.page_size+i)
                });
                if(data.length<obj.page_size){
                    $('.infinite-scroll-preloader .preloader').remove();
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                }else{
                    $('.infinite-scroll-preloader').html('');
                    $('.infinite-scroll-preloader').append('<div class="preloader"></div>')
                }
            }
        });
    },
//    倒计时2
    showtime2 : function(time,obj2,i){
        setInterval(function(){
             time--
            if(time>0){
                var date  =  parseInt(time/(60*60*24));
                var hours   =  parseInt(time/(60*60)%24);
                var minute= parseInt(time/60%60);
                var second=  parseInt(time%60);
                //timehtml='';
                obj2.eq(i).find('.time').html('<div class="date public"><span>0</span>'+date+'</div><div class="dd">:</div><div class="hour public"><span>0</span>'+hours+'</div><div class="dd">:</div><div class="minute public"><span>0</span>'+minute+'</div><div class="dd">:</div><div class="second public"><span>0</span>'+second+'</div><div class="dd"></div>');
                if(second>9){
                    obj2.eq(i).find('.time').children('.second').find('span').hide();
                }
                if(minute>9){
                    obj2.eq(i).find('.time').children('.minute').find('span').hide();
                }
                if(hours>9){
                    obj2.eq(i).find('.time').children('.hour').find('span').hide();
                }
                if(date>9){
                    obj2.eq(i).find('.time').children('.date').find('span').hide();
                }
            }else{
                obj.eq(i).find('.time').html('<div class="date public"><span>00</span></div><div class="dd">:</div><div class="hour public"><span>00</span></div><div class="dd">:</div><div class="minute public"><span>00</span></div><div class="dd">:</div><div class="second public"><span>00</span></div><div class="dd"></div>')
            }
        },1000);
    },
//    搜索列表
    search : function(url,obj){
        shopping.get(url,obj,function(res){
            if(res.ret==0){
                console.log(res);
                var list = res.data;
                if(list==''&&obj.page_index==1){
                    $('.infinite-scroll-preloader').remove();
                    var keyword = publicJs.getQueryString('keyword');
                    publicJs.pagenull('抱歉，没有找到与“'+keyword+'”相关的商品！');
                }
                if(list.length<obj.page_size){
                    $('.infinite-scroll-preloader .preloader').remove();
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                }else{
                    $('.infinite-scroll-preloader').html('');
                    $('.infinite-scroll-preloader').append('<div class="preloader"></div>');
                }
                $.each(list,function(i,t){
                    $('.commodity_list').append('<li data-id="'+ t.product_id+'"><img class="comm_pic" src="'+ t.goods_thumb+'" alt=""><p class="p1">'+ t.product_name+'</p><div class="price"><p class="l">￥'+ t.unit_price+'</p><img class="r" src="images/icon1.png"></div><p class="buynum">'+ t.sales+'人已购买</p></li>')
                });
            }else{
                console.log(res.mag)
            }
        });
    },
//    订单详情
    orderdetail : function(url,obj){
        shopping.get(url,obj,function(res){
            if(res.ret==0){
                console.log(res);
                var d = res.data;
                $('.shopdetail .sub-button button').attr('data-pay_method',d.pay_method);
                if(d.type==15){
                    $('.shopdetail .orderinformation .com-num .l').html('服务类别：');
                    $('.shopdetail .orderinformation .com-num .r').html(d.category_name);
                }
                new Vue({
                    el:'#shopdetail',
                    data:{
                        product_list:d.product_list,
                        status_text : d.status_text,
                        order_no : d.order_no,
                        total_amount: d.total_amount,
                        user_name : d.user_name,
                        mobile : d.mobile,
                        address_desc : d.address_desc,
                        logistic_information : d.logistic_information,
                        reduce_price: d.reduce_price,
                        product_total_amount: d.product_total_amount,
                        postage_fee: d.postage_fee,
                        pay_method: d.pay_method,
                        order_id: d.order_id,
                        id: d.id,
                        status : false,
                        type : false
                    },
                    ready:function(){
                        this.show();
                        this.pay_type();
                        this.shwotype();
                    },
                    methods : {
                        show : function(){
                            if(d.status==1){
                                this.status = true
                            }
                        },
                        shwotype : function(){
                            if(d.type==14){
                                this.type = true
                            }
                        },
                        pay_type : function(){
                            if(d.pay_method=="alipay"){
                                this.pay_method = '支付宝';
                            }else{
                                this.pay_method = '微信';
                            }
                        }
                    }
                });
            }else{
                console.log(res.msg);
            }
        });
    }
}
